package statsyslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/stat"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"
	"time"

	"navi-mumbai/app/rpc/stat/internal/svc"
	"navi-mumbai/app/rpc/stat/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLastDayLtvLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLastDayLtvLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLastDayLtvLogic {
	return &UpdateLastDayLtvLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// update last day ltv
func (l *UpdateLastDayLtvLogic) UpdateLastDayLtv(in *pb.UpdateLastDayLtvReq) (*pb.UpdateLastDayLtvResp, error) {
	defer utils.RecoverPanic()

	var start, end *time.Time
	if in.StartTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start = &t
	}
	if in.EndTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end = &t
	}

	var statDay int64
	var err error
	var startUid, endUid int64
	var loginUserCnt int64
	var idx int
	var st, et time.Time
	for idx = 30; idx > 1; idx-- {
		statDay, _ = strconv.ParseInt(start.AddDate(0, 0, -(idx-1)).Format(dbkey.DateFormat2), 10, 64)
		_, err = l.svcCtx.LifeTimeValueModel.FindOne(l.ctx, statDay)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			if err != stat.ErrNotFound {
				l.Logger.Errorf("err == ", zap.Error(err))
			}
			continue
		}

		st = start.AddDate(0, 0, -(idx - 1))
		et = end.AddDate(0, 0, -(idx - 1))
		// 查询用户起始uid
		startUid, err = l.svcCtx.UserAccountModel.FindFirstUid(l.ctx, &st, &et)
		if err != nil && err != stat.ErrNotFound {
			l.Logger.Errorf("err == ", zap.Error(err))
			continue
		}
		if startUid == 0 {
			continue
		}

		endUid, err = l.svcCtx.UserAccountModel.FindLastUid(l.ctx, &st, &et)
		if err != nil && err != stat.ErrNotFound {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		if endUid == 0 {
			continue
		}

		//
		loginUserCnt, err = l.svcCtx.UserLoginLogModel.CountLoginUser(l.ctx, start, end, startUid, endUid)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		if loginUserCnt == 0 {
			continue
		}

		//
		err = l.svcCtx.LifeTimeValueModel.UpdateLoginUserCnt(l.ctx, statDay, int64(idx), loginUserCnt)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			continue
		}
		startUid = 0
		endUid = 0
	}

	return &pb.UpdateLastDayLtvResp{}, nil
}
